package com.z.jdbc_demo;
import java.sql.*;

public class JdbcExample3 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2.获取数据库连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/zk?user=root&password=123456");
        // 3.创建Statement
        // 通过 ? 占位符来设置值
        PreparedStatement preparedStatement = connection.prepareStatement("select * from t_student where stu_id = ?");
        // 为占位符设置具体的值，索引从1开始，执行sql语句，获取结果
        preparedStatement.setInt(1, 2);
        // 4.执行编写的SQL，获取结果
        ResultSet resultSet = preparedStatement.executeQuery();
        // 5.处理结果
        System.out.println("ID"+ "\t" + "学生姓名" + "\t" + "分数"+ "\t\t" + "年龄");
        while (resultSet.next()) {
            int stuId = resultSet.getInt("stu_id");
            String stuName = resultSet.getString("stu_name");
            double stuScore = resultSet.getDouble("stu_score");
            int stu_age = resultSet.getInt("stu_age");
            System.out.println(stuId + "\t" + stuName + "\t\t" + stuScore + "\t" + stu_age);
        }
        // 6.释放资源
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
